<?php  
namespace app\index\controller;
use think\Controller;
class Login extends Controller{
	function login(){
		return view();
	}
	function register(){
		return view();
	}
	function findpass(){
		return view();
	}
	function user_add(){
		// halt(input());
		if(db('user')->insert(['username'=>$_POST['username'],'email'=>$_POST['email'],'password'=>md5($_POST['password'])])){
			$this->success('注册成功','login');
		}else{
			$this->error('注册失败','register');
		}
	}
	function user_login(){
		// halt(input());
		$res=db('user')->where('username',$_POST['loginname'])->whereor('email',$_POST['loginname'])->where('password',md5($_POST['password']))->where('limit','yes')->find();
		if(!captcha_check($_POST['captcha'])){
			$this->error('验证码错误','login');
		}
		if($res){
			db('user')->where('id',$res['id'])->update(['state'=>'1']);
			session('username',$res['username']);
			$this->success('登陆成功','index/index');
		}else{
			$this->error('用户名或密码错误，请重新登录','login');
		}
	}
	function user_exit(){
		db('user')->where('username',$_GET['username'])->update(['state'=>'0']);
		session('username',null);
		$this->success('账号已退出','index/index');
	}
	function findpass_check(){
		if(!captcha_check($_POST['captcha'])){
			$this->error('验证码错误','login');
		}
		$res=db('user')->where('username',$_POST['username'])->where('email',$_POST['email'])->update(['password'=>md5($_POST['password'])]);
		if($res){
			$this->success('重置密码成功','login');
		}else{
			$this->error('用户名或邮箱不存在，请重新填写','findpass');
		}
	}
}
?>